package com.sgcc.pda.hw.impl;

import android.content.Context;
import android.content.Intent;

import com.sgcc.pda.common.Callback;
import com.sgcc.pda.common.RequestManager;
import com.sgcc.pda.common.Value;


/**
 * @创建者：suven on 2020/3/24 11:46
 * @描述：图片模糊度判断
 * @版本：1.0
 * @修改人：
 * @修改时间：
 * @修改说明：
 * @修改版本：
 */
public class ImageOCRManager {

    public static final String IMAGE_PACKAGE_NAME = "com.sgcc.pda.img.ident";//图片识别包名
    public static final String IMAGE_SERVICE_NAME = IMAGE_PACKAGE_NAME + ".service.ImgIndentService";//图片识别服务名
    public static final String IMAGE_METHOD_NAME = "identify";//图片识别方法名
    public static final String IMAGE_PATH = "path";//图片识别添加路径
    public static final String IMAGE_SCORE = "score";//图片识别期望值

    static volatile ImageOCRManager mInstance;
    public RequestManager requestManager;
    private OnResultListener onResultListener;

    public ImageOCRManager() {
        requestManager = new RequestManager();
    }

    public static ImageOCRManager getInstance() {
        ImageOCRManager inst = mInstance;
        if (inst == null) {
            synchronized (ImageOCRManager.class) {
                inst = mInstance;
                if (inst == null) {
                    inst = new ImageOCRManager();
                    mInstance = inst;
                }
            }
        }
        return inst;
    }

    public void bind(Context context) {
        Intent intent = new Intent();
        intent.setClassName(IMAGE_PACKAGE_NAME,
                IMAGE_SERVICE_NAME);
        requestManager.bindService(context, intent);
    }

    public void unBind() {
        requestManager.unbind();
    }

    public void getImgIndentResult(String imgPath) {
        if (null != requestManager && requestManager.isBind()) {
            Value value = new Value();
            value.setMethodName(IMAGE_METHOD_NAME);//调用方法名
            value.addData(IMAGE_PATH, imgPath);//添加参数，图片路径
            requestManager.post(value, new Callback() {
                @Override
                public void callback(Value value) {
                    float result = value.getFloat(IMAGE_SCORE);
                    onResultListener.getResultData(result);
                }
            });
        } else {
            onResultListener.getResultData(-1);
        }
    }

    public interface OnResultListener {
        void getResultData(float result);
    }

    public void setOnResultListener(OnResultListener onResultListener) {
        this.onResultListener = onResultListener;
    }
}
